package org.jeecg.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializeFilter;
import org.jeecg.common.json.DateJsonSerializer;
import org.jeecg.common.json.SnakeCaseJsonSerializer;

import java.util.List;
import java.util.Map;

/**
 * json转换工具
 *
 * @author zhanglei01
 * @date 2022/03/10
 */
public class JsonUtils {
    /**
     * java bean转大写蛇形命名
     *
     * @param javaObject java对象
     * @return {@link Map}
     */
    public static Map javaBean2SnakeCase(Object javaObject) {
        SerializeFilter[] serializeFilters = new SerializeFilter[2];
        serializeFilters[0] = SnakeCaseJsonSerializer.getInstance();
        serializeFilters[1] = DateJsonSerializer.getInstance();
        String json = JSON.toJSONString(javaObject, serializeFilters);
        Map map = JSON.parseObject(json, Map.class);
        return map;
    }

    /**
     * list转大写蛇形命名
     *
     * @param list 列表
     * @return {@link List}<{@link Map}>
     */
    public static List<Map> list2SnakeCase(List list) {
        SerializeFilter[] serializeFilters = new SerializeFilter[2];
        serializeFilters[0] = SnakeCaseJsonSerializer.getInstance();
        serializeFilters[1] = DateJsonSerializer.getInstance();
        String json = JSON.toJSONString(list, serializeFilters);
        List<Map> maps = JSONArray.parseArray(json, Map.class);
        return maps;
    }
}
